﻿using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;

/**
* 
* Copyright (c) 2012 Anders Høst Kjærgaard | ahkj@itu.dk
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, 
* including without limitation the rights to use, copy, modify, merge, publish, distribute, 
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Flatland.Elements {
    /// <summary>
    /// Border class for Flatland.
    /// </summary>
    public class Border {
        private FlatlandWorld _flatland;
        private Vector2 _from, _to;
        public Body BorderBody { get; set; }    

        /// <summary>
        /// Border constructor
        /// </summary>
        /// <param name="flatland">Reference to Flatland</param>
        /// <param name="from">The from point f the border</param>
        /// <param name="to">The to point of the border</param>
        public Border(FlatlandWorld flatland, Vector2 from, Vector2 to) {
            _flatland = flatland;
            _from = from;
            _to = to;
            BorderBody = BodyFactory.CreateEdge(_flatland.World, _from, _to);
            BorderBody.BodyType = BodyType.Static;
            EdgeShape es = new EdgeShape(_from, _to);
            Fixture fixture = BorderBody.CreateFixture(es);
            fixture.CollisionCategories = Category.WALL_CATEGORY;
            fixture.CollidesWith = Category.All;
            fixture.OnCollision += BorderCollision;
        }

        public bool BorderCollision(Fixture a, Fixture b, Contact contact)
        {
            return true;
        }

        /// <summary>
        /// Draws the border to a batch.
        /// </summary>
        public void Draw() {
            _flatland.LineBatch.Begin(_flatland.Camera.SimProjection, _flatland.Camera.SimView);
            _flatland.LineBatch.DrawLine(_from, _to);
            _flatland.LineBatch.End();
        }
    }
}
